import common.commonFunction;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import updatePrestagingTable.updateprestagepopup;
[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
public var infoObject:Object={};

/**
 * This method is for getting the details
 * requered when the UI is loaded
 **/
[Bindable]public var urlPrefix:String;

public function onCreationComplete():void{
	
	editButton.enabled=false;
	deleteButton.enabled=false;
	urlPrefix=resourceManager.getString('Constants','url')+"/updatePrestaging/";
	infoObject["userId"]=new Date;
	getSetRecords.send(infoObject);
	
}

/** 
 * this method get program 
 * switch details from database
 **/
 
[Bindable] var recordsDetails:XML;
public var recordList:ArrayCollection; 
public function getSetRecordsDetails(event:ResultEvent):void{	
	
	recordsDetails=event.result as XML;	
 	
 	recordList=new ArrayCollection();
 	
 	for each(var o:Object in recordsDetails.Details){
			
			var sn:String=o.studentfname+" "+o.studentmname+" "+o.studentlname;
			var gen:String=o.gender;
			if(gen=="M"){
				gen="Male";
			}else{
				gen="Female";
			}
			recordList.addItem({select:false,studentId:o.studentId,regRollNo:o.regRollNo,enrollNo:o.enrollNo,
				dob:o.dob,category:o.category,categoryId:o.categoryId,genderName:gen,genderId:o.gender,
				studentfname:o.studentfname,studentmname:o.studentmname,
				studentlname:o.studentlname,fatherfname:o.fatherfname,fathermname:o.fathermname,
				fatherlname:o.fatherlname,motherfname:o.motherfname,mothermname:o.mothermname,
				motherlname:o.motherlname,newEntity:o.newEntity,newProgram:o.newProgram,
				newBranch:o.newBranch,newSpecialization:o.newSpecialization,newSemester:o.newSemester,
				newEntityId:o.newEntityId,newProgramId:o.newProgramId,
				newBranchId:o.newBranchId,newSpecializationId:o.newSpecializationId,newSemesterId:o.newSemesterId,
				attemptNumber:o.attemptNumber,processsedFlag:o.processsedFlag,
				registrationDueDate:o.registrationDueDate,semesterStartDate:o.semesterStartDate,
				semesterEndDate:o.semesterEndDate,primaryMail:o.primaryMail,processStatus:o.processStatus,reasoncode:o.reasoncode,
				description:o.description,
				perAddress:o.perAddress,perCity:o.perCity,perState:o.perState,perPincode:o.perPincode,
				corAddress:o.corAddress,corCity:o.corCity,corState:o.corState,corPincode:o.corPincode,officePhone:o.officePhone,
				extraPhone:o.extraPhone,otherPhone:o.otherPhone,fax:o.fax,admissionModee:o.admissionMode,studentName:sn});

		} 	

 	prestagingDetailsGrid.dataProvider=recordList;
	
}
  /**
 	* Mehtod to be called on request failure
 	**/
 public function onFailure(event:FaultEvent):void{
 	
 	Alert.show(commonFunction.getMessages('failure'),
 	commonFunction.getMessages('error'),4,null,null,errorIcon);
 	
 }
 
/** this method open edit program switch window **/
public function editRecord():void
{
	var selectedValues:ArrayCollection=commonFunction.getSelectedRowData(prestagingDetailsGrid);
	var s:String=selectedValues[0].admissionModee;
	if(s.toUpperCase()=="NEW"){
	
	var editWindow:updateprestagepopup=updateprestagepopup(PopUpManager.createPopUp(this,updateprestagepopup,true));
	editWindow.height=this.parentDocument.loaderCanvas.height;
	
	editWindow.admMode=selectedValues[0].admissionModee;
	editWindow.regNo=selectedValues[0].regRollNo;
	editWindow.enrNo=selectedValues[0].enrollNo;
	editWindow.atmNo=selectedValues[0].attemptNumber;
	editWindow.studentid.text=selectedValues[0].studentId;
	editWindow.regrollNo.text=selectedValues[0].regRollNo;
	editWindow.enrollNo.text=selectedValues[0].enrollNo;
	editWindow.dateofBirthField.text=selectedValues[0].dob;
	editWindow.category=selectedValues[0].categoryId;
	editWindow.catId=selectedValues[0].categoryId;
	editWindow.genderr=selectedValues[0].genderId;
	editWindow.studentFirstName.text=selectedValues[0].studentfname;
	editWindow.studentmiddleName.text=selectedValues[0].studentmname;
	editWindow.studentLastName.text=selectedValues[0].studentlname;
	editWindow.fatherFirstName.text=selectedValues[0].fatherfname;
	editWindow.fatherMiddleName.text=selectedValues[0].fathermname;
	editWindow.fatherLastName.text=selectedValues[0].fatherlname;
	editWindow.motherFirstName.text=selectedValues[0].motherfname;
	editWindow.motherMiddleName.text=selectedValues[0].mothermname;
	editWindow.motherLastName.text=selectedValues[0].motherlname;
	editWindow.entId=selectedValues[0].newEntityId;
	editWindow.prgId=selectedValues[0].newProgramId;
	editWindow.brnId=selectedValues[0].newBranchId;
	editWindow.spcId=selectedValues[0].newSpecializationId;
	editWindow.semId=selectedValues[0].newSemesterId;
	editWindow.atemptNo.text=selectedValues[0].attemptNumber;
	editWindow.admissionmodecombo.selectedItem=selectedValues[0].admissionModee.toString();
	editWindow.processedFlag.text=selectedValues[0].processsedFlag;
	editWindow.registrationDueDate.text=selectedValues[0].registrationDueDate;
	editWindow.semStartDate.text=selectedValues[0].semesterStartDate;
	editWindow.semEndDate.text=selectedValues[0].semesterEndDate;
	editWindow.primarymailtext.text=selectedValues[0].primaryMail;
	editWindow.processStatus.selectedItem=selectedValues[0].processStatus.toString();
	editWindow.reasonCode.text=selectedValues[0].reasoncode;
	editWindow.description.text=selectedValues[0].description;
	editWindow.permanentAddressField.addressLine1.text=selectedValues[0].perAddress;
	editWindow.permanentCity=selectedValues[0].perCity;
	editWindow.permanentState=selectedValues[0].perState;
	editWindow.permanentAddressField.pinField.text=selectedValues[0].perPincode;
	editWindow.corspondenceAddressField.addressLine1.text=selectedValues[0].corAddress;
	editWindow.corspondenceCity=selectedValues[0].corCity;
	editWindow.corspondenceState=selectedValues[0].corState;
	editWindow.corspondenceAddressField.pinField.text=selectedValues[0].corPincode;
	editWindow.phones.officePhoneField.text=selectedValues[0].officePhone;
	editWindow.phones.homePhoneField.text=selectedValues[0].extraPhone;
	editWindow.phones.otherPhoneField.text=selectedValues[0].otherPhone;
	editWindow.phones.faxPhoneField.text=selectedValues[0].fax;
	editWindow.buttonFunction=onCreationComplete;
	PopUpManager.centerPopUp(editWindow);
	}
	else{
		Alert.show(commonFunction.getMessages('thisRecordIsAvilableOnlyForDeletion'),commonFunction.getMessages('error'),4,null,null,errorIcon);
	}
}	
	

/** this method delete program switch details **/
public function deleteRecord():void
{
	Alert.show("del record send"+deleteRecords.url);
	var selectedValues:ArrayCollection=commonFunction.getSelectedRowData(prestagingDetailsGrid);
	
		infoObject["regRollNo"]="";
		infoObject["newEntityId"] ="";
		infoObject["newProgramId"] ="";
		infoObject["newBranchId"] ="";
		infoObject["newSpecializationId"] ="";
		infoObject["newSemesterId"] ="";
		infoObject["attemptNumber"] ="";
		infoObject["admissionMode"] ="";
		infoObject["enrollNo"] ="";
		
	for each(var obj:Object in selectedValues){
		
		infoObject["regRollNo"] =obj.regRollNo+"|";
		infoObject["newEntityId"] =obj.newEntityId+"|";
		infoObject["newProgramId"] =obj.newProgramId+"|";
		infoObject["newBranchId"] =obj.newBranchId+"|";
		infoObject["newSpecializationId"] =obj.newSpecializationId+"|";
		infoObject["newSemesterId"] =obj.newSemesterId+"|";
		infoObject["attemptNumber"] =obj.attemptNumber+"|";
		infoObject["admissionMode"] =obj.admissionModee+"|";
		infoObject["enrollNo"] =obj.enrollNo+"|";
	}
		deleteRecords.send(infoObject);	
}

/**
 * Method to be executed on successful deletion of selected records
 **/ 
public var successDetails:XML;
public function onSuccessfulEntry(event:ResultEvent):void{
	Alert.show("rohit");
	successDetails=event.result as XML;
	
	if(successDetails.Details.list_values=="success"){
	
	Alert.show((resourceManager.getString('Messages','deletedSuccessfully')),"Success",
		4,null,null,successIcon);
		
	}else{
		Alert.show(commonFunction.getMessages('failure'),
 	commonFunction.getMessages('error'),4,null,null,errorIcon);
	}		
		
		onCreationComplete();	
}